package c_4_3_3;

import java.lang.ref.SoftReference;

public class 引用 {

    //-Xmx10m
    public static void main(String[] args) {
        //user 是强引用
        User user = new User("this is id", "this is name");

        //reference 是软引用
        SoftReference<User> reference = new SoftReference<>(user);

        //reference 是弱引用
//        WeakReference<User> reference = new WeakReference<>(user);

        //reference 是虚引用
//        ReferenceQueue<User> queue = new ReferenceQueue<>();
//        PhantomReference<User> reference = new PhantomReference<>(user,queue);

        //强引用清除
        user = null;

        System.out.println("gc前：" + reference.get());
        System.gc();
        System.out.println("gc后：" + reference.get());

        //分配大数据触发系统自动gc
        byte[] bytes = new byte[1024 * 940 * 7];

        System.out.println("生成大对象，触发gc后：" + reference.get());


    }

    static class User {
        String id;
        String name;

        public User(String id, String name) {
            this.id = id;
            this.name = name;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    '}';
        }
    }


}
